import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:给定一个 N 叉树，返回其节点值的层序遍历。（即从左到右，逐层遍历）。
 *
 * 树的序列化输入是用层序遍历，每组子节点都由 null 值分隔
 * User: wangxin
 * Date: 2025-03-24
 * Time: 21:43
 */
class Node1 {
    public int val;
    public List<Node1> children;
}
public class Test2 {
    public List<List<Integer>> levelOrder(Node1 root) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(root == null){
            return res;
        }

        Queue<Node1> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int size = q.size();
            List<Integer> arr = new ArrayList<>();
            while(size > 0){
                Node1 n = q.poll();
                arr.add(n.val);
                func(q,n);
                size--;
            }
            res.add(arr);
        }
        return res;
    }
    public void func(Queue<Node1> q,Node1 root){
        List<Node1> c = root.children;
        for(int i = 0;i < c.size();i++){
            q.offer(c.get(i));
        }
    }
}
